package com.qianlou.springai01chat.controller;


import jakarta.annotation.Resource;
import org.springframework.ai.chat.messages.Media;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.openai.OpenAiChatOptions;
import org.springframework.ai.openai.api.OpenAiApi;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.util.MimeTypeUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

@SuppressWarnings("all")
@RestController
public class MultiModalityController {


    @Qualifier("openAiChatModel")
    @Resource
    private ChatModel client;

    /**
     * https://docs.spring.io/spring-ai/reference/api/chat/openai-chat.html#_multimodal
     *
     * @param msg      针对图片提出的问题
     * @param imageUrl 图片路径
     */
    @RequestMapping("/ai/multi")
    public Object multi(@RequestParam("msg") String msg, @RequestParam("imageUrl") String imageUrl) throws IOException {
        UserMessage userMessage = new UserMessage(msg, List.of(new Media(MimeTypeUtils.IMAGE_PNG, imageUrl)));
        ChatResponse response = client.call(new Prompt(
                List.of(userMessage),
                OpenAiChatOptions.builder().withModel(OpenAiApi.ChatModel.GPT_4_VISION_PREVIEW.getValue()).build()));
        return response.getResult().getOutput().getContent();
    }
}
